const express = require('express');
const app = express();
const port = 3000;
const {redisClient} = require('./redisConfig')

app.use(express.json());



// 引入下载逻辑
const { downloadFile } = require('./partitionDownload');
const { downloadAndUpload } = require('./download01');

// 定义一个简单的接口
app.get('/api/hello', (req, res) => {
    res.send('Hello, World!');
});


// 添加下载接口,添加redis分布式锁
// 在 Express 中使用 Redis 客户端
app.get('/set', async (req, res) => {
    await redisClient.set('test_key', 'Hello Redis');
    res.send('Key set successfully');
  });
  
// 添加下载接口,添加redis分布式锁
app.post('/api/download01', async (req, res) => {
    const url = req.body.url;
    console.log(`url: ${url}`);
    try {
        await downloadAndUpload(url);
        res.json({
            success: true,
            message: 'Download and upload completed successfully'
        });
    } catch (error) {
        res.status(500).json({
            success: false,
            message: 'Download and upload failed',
            error: error.message
        });
    }
  });

// 添加下载接口
app.get('/api/download', async (req, res) => {
    try {
        await downloadFile();
        res.send('Download completed successfully.');
    } catch (error) {
        res.status(500).send('Download failed: ' + error.message);
    }
});



// 启动服务器
app.listen(port, () => {
    console.log(`Server is running at http://localhost:${port}`);
}); 